/*
 * Copyright (c) 2012, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.bmi.gwt.ws.rebind;

import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.IncrementalGenerator;
import com.google.gwt.core.ext.RebindResult;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.TypeOracle;

public class PayloadSerializerInterfaceGenerator extends IncrementalGenerator
{
	private static final long PSERIALIZER_GENERATOR_VERSION_ID = 77L;

	@Override
	public RebindResult generateIncrementally(TreeLogger logger, GeneratorContext ctx, String requestedClass)
			throws UnableToCompleteException
	{
		TypeOracle typeOracle = ctx.getTypeOracle();
		assert (typeOracle != null);

		JClassType serializer = typeOracle.findType(requestedClass);
		if (serializer == null)
		{
			logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + requestedClass + "'", null);
			throw new UnableToCompleteException();
		}

		if (serializer.isInterface() == null)
		{
			logger.log(TreeLogger.ERROR, serializer.getQualifiedSourceName() + " is not an interface", null);
			throw new UnableToCompleteException();
		}

		PayloadSerializerCreator serializerCreator = createSerializerCreator(serializer);

		TreeLogger serializerLogger = logger.branch(TreeLogger.DEBUG, "Generating serializer for interface '"
				+ serializer.getQualifiedSourceName() + "'", null);

		return serializerCreator.create(serializerLogger, ctx);
	}

	protected PayloadSerializerCreator createSerializerCreator(JClassType serializer)
	{
		return new PayloadSerializerCreator(serializer);
	}

	@Override
	public long getVersionId()
	{
		return PSERIALIZER_GENERATOR_VERSION_ID;
	}
}
